import java.util.Arrays;
import java.util.Stack;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/2/23
 * \* Time: 20:17
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class TestSort {
    /**
     * 插入排序
     * @param array
     */
    public static void insertSort(int[] array){
        for(int i = 0; i < array.length; i++){
            int temp = array[i];
            int j = i-1;
            for(; j >= 0; j--){
                if(array[j] > temp){
                    array[j+1] = array[j];
                }else {
                    break;
                }

            }
            array[j+1] = temp;
        }
    }

    public static void shell(int[] array, int gap){
        for(int i = 0; i<array.length; i++){
            int tmp = array[i];
            int j = i - gap;
            for(; j > 0; j--){
                if(array[j] > tmp){
                    array[j+gap] = array[j] ;
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    public static void shellSort(int [] array){
        int gap = array.length;
        while(gap >1){
            shell(array , gap);
            gap /= 2;
        }
        shell(array,1);
    }



    public static void swap(int[] array, int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] =tmp;
    }

    public static void selectSort1(int[] array){
        for(int i = 0; i < array.length; i++){
            for(int j = i+1; j < array.length; j++){
                if(array[i] > array[j]){
                    swap(array , i , j);
                }
            }
        }
    }
    public static void selectSort2(int[] array){

    }

    public static void heapSort(int[] array){
        createHeap(array);
        int end = array.length -1 ;
        while (end > 0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    public static void createHeap(int[] array){
        for(int parent = (array.length-1-1)/2; parent >= 0 ; parent--){
            shiftDown(array, parent, array.length);
        }
    }
    public static void shiftDown(int[] array, int parent, int len){
        int child = 2*parent + 1;
        while(child < len){
            if(child + 1 < len && array[child] < array[child+1]){
                if(array[child] > array[parent]){
                    swap(array, child,parent);
                    parent = child;
                    child = 2*parent + 1;
                }
            }
        }
    }
    
    public static void bubbleSort1(int [] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length -1-i; j++) {
                if(array[j] > array[j+1]){
                    swap(array,j+1,j);
                }
            }
        }
    }

    public static void bubbleSort2(int [] array){
        for (int i = 0; i < array.length ; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]){
                    swap(array,j+1,j);
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    }

    public static void quickSort(int[] array){
        quick(array,0,array.length-1);
    }

    public static void insettSort2(int []array,int start, int end){
        for (int i = 1; i <= end; i++) {
            int tmp = array[i];
            int j = i-1;

            for (; j >= start; j--) {
                if(array[j] >= tmp){
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }

    }

    public static void quick(int[] array,int left,int right){
        if(left >= right){
            return;
        }
        if(right - left + 1 <= 70){
            insettSort2(array,left,right);
            return;
        }


        int midValIndex = findMidValIndex(array,left,right);
        swap(array,midValIndex,left);
        int pivot = partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array, pivot + 1, right);
    }

    public static int findMidValIndex(int[] array,int start , int end){
        int mid = start + ((end - start) >>> 1);
        if (array[start] < array[end]){
            if(array[start] > array[mid]){
                return start;
            }else if(array[mid] < array[end]){
                return mid;
            }else {
                return end;
            }
        }else {
            if (array[start] < array[mid]) {
                return start;
            } else if (array[mid] < array[end]) {
                return end;
            } else {
                return mid;
            }
        }
    }

    private static int partition(int[] array,int start, int end){
        int tmp = array[start];
        while(start < end){
            while(start < end && array[end] >= tmp){
                end--;
            }
            array[start] = array[end];
            while (start < end && array[start] <= tmp){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }

    public static void quickSort1(int[]array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int pivot = partition(array,left,right);
        if(pivot > left+1) {
            //左边有2个元素
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot < right-1) {
            //右边有2个元素
            stack.push(pivot+1);
            stack.push(right);
        }

        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();

            pivot = partition(array,left,right);

            if(pivot > left+1) {
                //左边有2个元素
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot < right-1) {
                //右边有2个元素
                stack.push(pivot+1);
                stack.push(right);
            }
        }

    }

    public static void mergeSort1(int[] array){
        mergeSortInternal(array,0,array.length-1);
    }

    public static void mergeSortInternal(int[] array,int low,int high){
        if (low >= high) return;

        int mid = low + ((high - low) >>> 1);
        mergeSortInternal(array,low,mid);
        mergeSortInternal(array,mid+1,high);

        merge(array,low,mid,high);
    }

    private static void merge(int[] array,int low,int mid,int high) {
        int[] tmp = new int[high-low+1];
        int k = 0;//

        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 =  high;

        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }

        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }

        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //拷贝tmp数组的元素 放入原来的数组array当中
        for (int i = 0; i < k; i++) {
            array[i+low] = tmp[i];
        }
    }

    public static void mergeSort(int[] array){
        int nums = 1;
        while(nums < array.length){
            for (int i = 0; i < array.length; i += nums*2) {
                int left = i;
                int mid = left + nums -1;
                if(mid >= array.length){
                    mid = array.length-1;
                }
                int right = mid+nums;
                if(right >=array.length){
                    right = array.length -1;
                }
                merge(array,left,mid,right);
            }
            nums *= 2;

        }
    }



    public static void main(String[] args) {
        //int[] array = {12,5,18,10,4,2};
        //int[] array = {3,44,38,5,47,15,36,26};
        int[] array = {6,12,10,3,5};
        //insertSort(array);
        //bubbleSort1(array);
       // bubbleSort2(array);
        //quickSort(array);
       // quickSort1(array);
        mergeSort1(array);
        System.out.println(Arrays.toString(array));
    }

}
